package cn.edu.njust.service.impl;

import cn.edu.njust.core.UserContext;
import cn.edu.njust.core.enums.LeaveStatusEnum;
import cn.edu.njust.mapper.LeaveMapper;
import cn.edu.njust.pojo.bo.LeaveBO;
import cn.edu.njust.pojo.bo.PageBean;
import cn.edu.njust.pojo.po.Leaves;
import cn.edu.njust.service.LeaveService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求业务层实现类
 */
@Service
@RequiredArgsConstructor
public class LeaveServiceImpl implements LeaveService {

    private final LeaveMapper leaveMapper;

    @Override
    public void addLeave(LeaveBO leaveBO) {

        Integer currentUserId = UserContext.getCurrentUserId();
        LocalDateTime now = LocalDateTime.now();

        Leaves leave = BeanUtil.copyProperties(leaveBO, Leaves.class);
        leave.setUserId(currentUserId);
        leave.setUsername(UserContext.getCurrentUserName());
        leave.setUpdateTime(now);
        leave.setUpdateBy(currentUserId);
        leave.setCreateTime(now);
        leave.setCreateBy(currentUserId);
        leaveMapper.addLeave(leave);
    }

    @Override
    public PageBean<Leaves> pageQueryList(int pageNum, int pageSize) {

        int begin = (pageNum - 1) * pageSize;
        List<String> statusList = ListUtil.of(LeaveStatusEnum.APPLY.getCode());
        List<Leaves> rows = leaveMapper.pageQueryList(begin, pageSize, statusList);
        int totalCount = leaveMapper.selectTotalCount(statusList);
        return PageBean.of(totalCount, rows);
    }

}
